package com.cxh.supermarket.web.controller.labor;

import cn.dev33.satoken.annotation.SaIgnore;
import com.cxh.supermarket.common.annotation.RepeatSubmit;
import com.cxh.supermarket.common.constant.Constants;
import com.cxh.supermarket.common.core.domain.R;
import com.cxh.supermarket.common.core.domain.model.SmsLoginBody;
import com.cxh.supermarket.system.service.SysLoginService;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * app端登录
 *
 * @author zxl
 * @since 2024/1/22
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/labor/mobile")
public class LaborLoginController {
  private final SysLoginService loginService;


  /**
   * 短信登录(无账号自动注册)
   *
   * @param smsLoginBody 登录信息
   * @return 结果
   */
  @SaIgnore
  @PostMapping("/smsLogin")
  public R<Map<String, Object>> smsLogin(@Validated @RequestBody SmsLoginBody smsLoginBody) {
    Map<String, Object> ajax = new HashMap<>();
    // 生成令牌
    String token = loginService.smsLoginOrRegister(smsLoginBody.getPhonenumber(), smsLoginBody.getSmsCode());
    ajax.put(Constants.TOKEN, token);
    return R.ok(ajax);
  }


  /**
   * app一键登录(无账号自动注册)
   *
   * @param accessToken 阿里云token
   * @return 结果
   */
  @PostMapping("/appOneClick")
  public R<Map<String, Object>> appOneClick(String accessToken) {
    Map<String, Object> ajax = new HashMap<>();
    // 生成令牌
    String token = loginService.appOneClick(accessToken);
    ajax.put(Constants.TOKEN, token);
    return R.ok(ajax);
  }


  /**
   * 小程序登录
   *
   * @param code 小程序登陆code
   * @return 结果
   */
  @PostMapping("/xcxLogin")
  public R<Map<String, Object>> xcxLogin(@NotBlank(message = "小程序登录code不能为空") String code) {
    return R.ok(loginService.xcxLogin(code));
  }

  /**
   * 第一次小程序登录,绑定手机号
   *
   * @param loginCode 小程序登陆凭证
   * @param phoneCode 小程序获取手机号凭证
   * @return token
   */
  @PostMapping("/xcxFirstLogin")
  @RepeatSubmit(interval = 10, timeUnit = TimeUnit.SECONDS, message = "正在登陆,请稍后")
  public R<Map<String, Object>> xcxFirstLogin(@NotBlank(message = "小程序登录code不能为空") String loginCode,
                                              @NotBlank(message = "小程序获取手机号code不能为空") String phoneCode) {
    return R.ok(loginService.xcxFirstLogin(loginCode, phoneCode));

  }

  /**
   * 获取发布招工人（职位）手机号
   *
   * @param userId 用户id
   * @return 结果
   */
  @GetMapping("/jobPhonenumber")
  public R<String> jobPhonenumber(@NotNull(message = "用户id不能为空") Long userId) {
    return R.ok("", loginService.jobPhonenumber(userId));
  }



}
